package com.briup.cms.web.controller;

import com.briup.cms.bean.Role;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.RoleService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import com.briup.cms.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "Role")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService service;

    //注册一个角色
    @ApiOperation(value = "注册角色",notes = "传递一个json对象的字符串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/saveRole")
    public Result saveRole(@RequestBody Role role, HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role1 = (int) info.get("role");
        if (role1 != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.saveOrUpdateRole(role);
        return Result.success(role);
    }

    //根据角色name查找具体角色信息
    @ApiOperation(value = "查找角色",notes = "传递一个字符串的name")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/findRoleByName")
    public Result findRoleByName(String name){
        Role role = service.findRoleByName(name);
        if (role==null){
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        return Result.success(role);
    }

    //查找所有角色信息
    //可以传入第几页
    @ApiOperation(value = "分页查找所有角色",notes = "传入第几页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @GetMapping("/findAll")
    public Result findAll(Integer pageNum){
        Page<Role> page = service.findAll(pageNum,20);
        return Result.success(page);
    }

    @ApiOperation(value = "通过Id删除对象",notes = "传入要删除对象的Id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @DeleteMapping("/deleteRoleById/{id}")
    public Result deleteRoleById(@PathVariable Long id,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteRoleById(id);
        return Result.success("单个删除成功");
    }

    @ApiOperation(value = "通过Id批量删除对象",notes = "传入要删除对象的Id的集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "JWT",
                    dataType = "String",required = true,
                    paramType = "header"),
    })
    @PostMapping("/deleteRoleInBatch")
    public Result deleteRoleInBatch(@RequestBody List<Long> ids,HttpServletRequest request){
        String token = request.getHeader("token");
        Map<String, Object> info = JwtUtil.getInfo(token);
        int role = (int) info.get("role");
        if (role != 1){
            throw new ServiceException(ResultCode.ROLE_NO);
        }
        service.deleteRoleInBatch(ids);
        return Result.success("批量删除成功");
    }


}
